package com.nanjia.boot.product.spec.group.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.product.spec.group.model.form.SpecGroupForm;
import com.nanjia.boot.product.spec.group.model.query.SpecGroupQuery;
import com.nanjia.boot.product.spec.group.model.vo.SpecGroupVO;
import com.nanjia.boot.product.spec.group.service.SpecGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 商品规格组前端控制层
 *
 * @author 周洋
 * @since 2025-07-09 15:30
 */
@Tag(name = "商品规格组接口")
@RestController
@RequestMapping("/api/v1/spec-group")
@RequiredArgsConstructor
public class SpecGroupController {

    private final SpecGroupService specGroupService;

    @Operation(summary = "商品规格组分页列表")
    @GetMapping("/page")
   //@PreAuthorize(@ss.hasPerm('spec:group:query')")
    public PageResult<SpecGroupVO> getSpecGroupPage(SpecGroupQuery queryParams) {
        IPage<SpecGroupVO> result = specGroupService.getSpecGroupPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增商品规格组")
    @PostMapping
   //@PreAuthorize(@ss.hasPerm('spec:group:add')")
    public Result<Void> saveSpecGroup(@RequestBody @Valid SpecGroupForm formData, HttpServletRequest request) {
        boolean result = specGroupService.saveSpecGroup(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取商品规格组表单数据")
    @GetMapping("/{id}/form")
   //@PreAuthorize(@ss.hasPerm('spec:group:edit')")
    public Result<SpecGroupForm> getSpecGroupForm(
            @Parameter(description = "商品规格组ID") @PathVariable Long id
    ) {
        SpecGroupForm formData = specGroupService.getSpecGroupFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改商品规格组")
    @PutMapping(value = "/{id}")
   //@PreAuthorize(@ss.hasPerm('spec:group:edit')")
    public Result<Void> updateSpecGroup(
            @Parameter(description = "商品规格组ID") @PathVariable Long id,
            @RequestBody @Validated SpecGroupForm formData, HttpServletRequest request
    ) {
        boolean result = specGroupService.updateSpecGroup(id, formData,IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "删除商品规格组")
    @DeleteMapping("/{ids}")
   //@PreAuthorize(@ss.hasPerm('spec:group:delete')")
    public Result<Void> deleteSpecGroups(
            @Parameter(description = "商品规格组ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = specGroupService.deleteSpecGroups(ids);
        return Result.judge(result);
    }
}
